<template>
	<view>

		<view class="head-top">
			<u-gap height="90" bg-color="#fff"></u-gap>
			<view class="head-nav">
				<view class="back" @tap="backPage"><u-icon name="arrow-left" size="32" color="#272636"></u-icon></view>
				<view class="search-box"><u-search placeholder="请输入关键字" :disabled="true" @click="searchGoods" :show-action="false"></u-search></view>
				<view class="jumpTo" @tap="jumpPage()">
					<text>分类</text>
					<u-image width="32rpx" height="32rpx" :src="appletImgUrl+'cate_tow.png'"></u-image>
				</view>
			</view>
			<view class="tab">
				<view :class="[tabIndex == index && index != 1 ? 'active' : '']" v-for="(item,index) in tabs"
					:key="index" @click="choiceTab(index)">
					<view class="">{{item}}</view>
					<view class="icon" v-if="tabIndex == 1 && index == 1">
						<view @click.stop="sortPrice = 'DESC';sortList()">
							<u-icon top="-13" size="20" :color="sortPrice == 'DESC' ? '#FF4D46' : '#aaaaaa'"
								name="arrow-up-fill"></u-icon>
						</view>
						<view @click.stop="sortPrice = 'ASC';sortList()">
							<u-icon top="-13" size="20" :color="sortPrice == 'ASC' ? '#FF4D46' : '#aaaaaa'"
								name="arrow-down-fill"></u-icon>
						</view>
					</view>
				</view>
				<view class="changeStyle">
					<u-image width="32rpx" height="32rpx" :src="appletImgUrl+'layout-one.png'" @tap="tabLay(1)"
						v-if="isLayout"></u-image>
					<u-image width="32rpx" height="32rpx" :src="appletImgUrl+'layout-two.png'" @tap="tabLay(2)"
						v-else></u-image>
				</view>
			</view>
		</view>
		<view class="loading" v-if="list == '' && !fail">
			<u-loading mode="circle"></u-loading>
		</view>

		<view v-if="fail" class="" style="padding-top: 300rpx;">
			<u-empty mode="search"></u-empty>
		</view>

		<template v-if="list != ''">

			<u-gap height="20"></u-gap>
			<!--一行一个-->
			<view class="list transverse" v-if="isLayout">
				<block v-for="(item,index) in list" :key="item.id">
					<navigator :url="`/pages/productDetail/productDetail?id=${item.id}&mainGoodsType=${mainGoodsType}`" class="item">

						<view class="pro-img">
							<u-lazy-load height="200rpx" width="200rpx" img-mode="aspectFill" border-radius="20"
								:image="item.goodsMainPhoto"></u-lazy-load>
						</view>
						<view class="info">
							<view class="name">{{item.goodsName}}</view>
							<view class="price-box" v-if="item.isExchangeGood==0">
								<view class="price">￥<text>{{item.salePrice.toFixed(2)}}</text></view>
								<view class="btn">
									去购买
								</view>
							</view>
							<view class="new-price-box" v-if="item.isExchangeGood==1">
								<view class="integral-price-one" v-if="item.redDeductible>=item.salePrice">
									<view class="inter-num">{{item.redIntegral}}</view>
									<view>红包积分</view>
								</view>
								<view class="integral-price-two" v-if="item.redDeductible<item.salePrice">
									<view class="integral-price-left">
										<view>￥</view><view class="after-price">{{item.salePrice-item.redDeductible+item.redIntegral}}</view>
									</view>
									<view class="addIcon-two">+</view>
									<view class="jifen-tow">{{item.redIntegral}}</view>
									<view class="red-word">红包积分</view>
									<!-- <view class="integral-price-right" v-if="item.redDeductible<item.salePrice">红包积分:{{item.redIntegral}}</view> -->
								</view>
								<view class="old-price">
									原价:￥{{item.salePrice.toFixed(2)}}
								</view>
							</view>
						</view>
					</navigator>
				</block>
				<u-loadmore margin-top="30" :status="status" />
			</view>
			<!--一行两个-->
			<view class="Vertical-goodsList" v-else>
				<view class="Vertical-goodsList-box">
					<block v-for="(item,index) in list" :key="item.id">
						<navigator :url="`/pages/productDetail/productDetail?id=${item.id}&mainGoodsType=${mainGoodsType}`" class="item">

							<view class="pro-img">
								<u-lazy-load height="334rpx" width="334rpx" img-mode="aspectFill" border-radius="20"
									:image="item.goodsMainPhoto"></u-lazy-load>
							</view>
							<view class="info">
								<view class="name">{{item.goodsName}}</view>
								<view class="ppo" v-if="item.isExchangeGood==1">
									<view class="price-box">
										<view class="integral-box" v-if="item.redDeductible>=item.salePrice">
											<view class="integral-num">{{item.redIntegral}}</view>
											<view>红包积分</view>
										</view>
										<view class="integral-deduction-box" v-if="item.redDeductible<item.salePrice">
											<view>￥</view>
											<view class="deduction-num">
												<view class="deduction-num-price">{{item.salePrice-item.redDeductible+item.redIntegral}}</view>
												<view class="addIcon">+</view>
												<view class="redIntegral_n">{{item.redIntegral}}</view>
												<view class="in-word">红包积分</view>
											</view>
									</view>
										<view class="bottom-price">
											<!-- <view class="allow-exchange" v-if="item.redDeductible<item.salePrice">
												<view class="jifen">{{item.redIntegral}}</view>
												<view class="jifen-word">红包积分</view>
												
											</view> -->
											<view class="oldPrice-box">
												<view class="label-tit">
													原价￥{{item.salePrice.toFixed(2)}}</view>
											</view>
										</view>
									</view>

								</view>
								<view class="price-box-two" v-if="item.isExchangeGood==0">
									<view class="price">￥<text>{{item.salePrice.toFixed(2)}}</text></view>
									<!-- <view class="btn">
										去购买
									</view> -->
								</view>

							</view>
						</navigator>
					</block>
				</view>

				<u-loadmore margin-top="30" :status="status" />
			</view>
		</template>
	</view>
</template>

<script>
	let key = ''
	import {appletImgUrl} from "@/common/config.js"
	export default {
		data() {
			return {
				appletImgUrl:appletImgUrl,
				list: '',
				status: 'loading',
				tabIndex: 0,
				sortPrice: 'ASC',
				tabs: [
					'综合',
					'价格',
					'新品'
				],
				fail: false,
				isLayout: true,
				curLay: 1,
				keyword: "",
				mainGoodsType: null //1:VIP区，2：优品区，3：爆品区，4：兑换区,5：平价区
			};
		},
		onPullDownRefresh() {
			this.pageNo = 1
			this.status = 'loading'
			this.fail = false
			this.getProductList(res => {
				uni.stopPullDownRefresh()
				if (res.list == '') {
					this.fail = true
					return
				}
				if (res.list.length < 10) {
					this.status = 'nomore'
				} else {
					this.status = 'loadmore'
				}
				this.list = res.list
			}, error => {
				this.fail = true
				uni.stopPullDownRefresh()
			})
		},
		methods: {
			backPage() {
				uni.navigateBack();
			},
			searchGoods() {
				console.log(22222)
                 this.$u.route('/pages/other/search/search');
			},
			jumpPage() {
				uni.switchTab({
					url: '/pages/tabBar/goodsType/goodsType'
				});
			},
			tabLay(val) {
				this.isLayout = !this.isLayout;
				this.curLay = val;
			},
			sortList() {
				this.pageNo = 1
				this.status = 'loading'
				this.fail = false
				this.getProductList(res => {
					if (res.list == '') {
						this.fail = true
						return
					}
					if (res.list.length < 10) {
						this.status = 'nomore'
					} else {
						this.status = 'loadmore'
					}
					this.list = res.list
				}, error => {
					this.fail = true
				})
			},
			choiceTab(index) {
				if (this.tabIndex != index) {
					this.tabIndex = index
					this.pageNo = 1
					this.status = 'loading'
					this.list = ''
					this.fail = false
					this.getProductList(res => {
						if (res.list == '') {
							this.fail = true
							return
						}
						if (res.list.length < 10) {
							this.status = 'nomore'
						} else {
							this.status = 'loadmore'
						}
						this.list = res.list
					}, error => {
						this.fail = true
					})
				}
			},
			getProductList(successCallBack, errorCallBack) {
				this.$u.post('/goods/list', {
					pageNo: this.pageNo,
					pageSize: 10,
					goodsName: this.goodsClassId ? '' : key,
					goodsClassId: this.goodsClassId || '',
					sortPrice: this.sortPrice,
					sortAddTime: this.tabIndex == 2 ? 1 : '',
					mainGoodsType: this.mainGoodsType
				}).then(res => {
					if (res.list != '') {
						res.list.forEach(item => {
							let transformationArray = item.goodsMainPhoto.split(',');
							item.goodsMainPhoto = this.imageURL + transformationArray[0];
							if(item.redIntegral&&item.redDeductible&&item.redIntegral>0){
								item.isExchangeGood=1;//兑换区商品
							}else{
								item.isExchangeGood=0;//非兑换区商品
							}
						})
					}
					successCallBack(res)
				}).catch(error => {
					errorCallBack(error)
				})
			}
		},
		onReachBottom() {
			if (this.status == 'loading' || this.status == 'nomore') return
			this.pageNo++
			this.status = 'loading'
			this.getProductList(res => {
				if (res.list == '') {
					this.status = 'nomore'
					return
				}
				this.list = this.list.concat(res.list)
				this.$nextTick(() => {
					this.status = 'loadmore'
				})
			})
		},
		onLoad(options) {
			this.pageNo = 1
			this.goodsClassId = options.goodsClassId;
			this.mainGoodsType = options.mainGoodsType;
			uni.setNavigationBarTitle({
				title: options.key
			})
			key = options.key
			this.getProductList(res => {
				if (res.list == '') {
					this.fail = true
					return
				}
				if (res.list.length < 10) {
					this.status = 'nomore'
				} else {
					this.status = 'loadmore'
				}
				this.list = res.list
			}, error => {
				this.fail = true
			})
		}
	}
</script>

<style lang="scss">
	.tab {
		display: flex;
		background-color: #fff;
		// border-bottom: 1rpx solid rgba($color: #000000, $alpha: .1);
		align-items: center;
		height: 80rpx;
		// border-top: 1rpx solid rgba($color: #000000, $alpha: .1);
		box-shadow: 0px 0 12px 1px rgba(203, 203, 202, 0.2);

		>view {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			text-align: center;

			&.active {
				color: #FF4D46;
			}

			.icon {
				margin-left: 20rpx;

				>view {
					height: 20rpx;
				}
			}
		}
	}

	.loading {
		display: flex;
		justify-content: center;
		padding-top: 300rpx;
	}

	.list {
		padding: 0 30rpx;
		padding-bottom: 30rpx;

		.item {
			display: flex;

			&:first-child {
				margin-top: 0;
			}

			box-shadow: 0px 0 12px 1px rgba(203, 203, 202, 0.2);
			margin-top: 20rpx;
			border-radius: 20rpx;

			.pro-img {
				width: 200rpx;
			}

			.info {
				flex: 1;
				margin-left: 27rpx;
				// height: 280rpx;
				position: relative;
				

				.price-box {
					width: 100%;
					position: absolute;
					bottom: 10rpx;
					left: 0;


					.btn {
						color: #FFFFFF;
						background: linear-gradient(90deg, #FF3768, #FC6161);
						width: 120rpx;
						border-radius: 20rpx;
						text-align: center;
						font-size: 24rpx;
						padding-top: 4rpx;
						padding-bottom: 4rpx;
						margin-top: 4rpx;
					}
				}

				.price {
					font-size: 24rpx;
					color: #FF4E47;
					margin-top: 17rpx;
					font-weight: 600;

					text {
						font-size: 32rpx;
					}
				}

				.sales {
					color: #AAAAAA;
					font-size: 22rpx;
				}

				.tag {
					color: #fff;
					display: flex;
					justify-content: space-between;
					width: 300rpx;
					padding: 3rpx 15rpx;
					// display: inline-block;
					font-size: 26rpx;
					background: url('~@/static/product-list-tag-bg.png')no-repeat center / 100% 100%;

					&:first-child {
						margin-bottom: 10rpx;
					}

					text {
						color: #FF4D46;
					}
				}

				.name {
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					font-weight: 500;
					font-size: 28rpx;
					margin-bottom: 10rpx;
					overflow: hidden;
					margin-top: 20rpx;

					text {
						color: #fff;
						font-size: 26rpx;
						padding: 2rpx 10rpx;
						margin-right: 10rpx;
						background: linear-gradient(90deg, #FF3768, #FC6161);
						border-radius: 6rpx;
					}
				}
			}
		}
	}

	.Vertical-goodsList {
		.Vertical-goodsList-box {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			justify-content: flex-start;
			margin-left: 30rpx;
			margin-right: 30rpx;

			.item {
				width: 334rpx;
				margin-bottom: 20rpx;

				.pro-img {
					width: 334rpx;
				}

				.name {
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					font-weight: 500;
					font-size: 28rpx;
					margin-bottom: 10rpx;
					overflow: hidden;
					margin-top: 20rpx;
				}

				.price-box {
					.integral-box {
						font-size: 22rpx;
						color: #FF1C60;
						display: flex;
						flex-direction: row;
						justify-content: flex-start;
						line-height: 48rpx;

						.integral-num {
							font-size: 28rpx;
							font-weight: bold;
							margin-right: 4rpx;
						}
					}

					.integral-deduction-box {
						font-size: 24rpx;
						color: #FF1C60;
						display: flex;
						flex-direction: row;
						justify-content: flex-start;
						line-height: 48rpx;

						.deduction-num {
							display: flex;
							flex-direction: row;
							justify-content: flex-start;
							font-size: 20rpx;

							.deduction-num-price {
								font-size: 28rpx;
								font-weight: bold;
							}
							.addIcon{
								font-size: 22rpx;
								font-weight: bold;
							}
							.redIntegral_n{
								font-size: 28rpx;
								font-weight: bold;
							}
							.in-word{
								font-size: 22rpx;
								margin-left: 10rpx;
							}
						}
					}

					.bottom-price {
						display: flex;
						flex-direction: row;
						justify-content: flex-start;

						.allow-exchange {
							font-size: 20rpx;
							background: linear-gradient(to right, #FFA858, #E42548);
							color: #fff;
							line-height: 42rpx;
							padding-left: 10rpx;
							padding-right: 10rpx;
							margin-right: 8rpx;
							border-radius: 12rpx;

						}

						.oldPrice-box {
							.label-tit {
								text-decoration: line-through;
								color: #C2C2C2;
								font-size: 20rpx;
								// line-height: 42rpx;
							}
						}
					}


				}
			}
		}

		.Vertical-goodsList-box .item:nth-child(odd) {
			margin-right: 20rpx;
		}
	}

	.head-nav {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding-left: 30rpx;
		padding-right: 30rpx;
		height: 88rpx;
		background-color: #fff;
		// position: sticky;
		// top: 0;
		z-index: 1000;
		width: 570rpx;

		.jumpTo {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			padding-top: 28rpx;

			text {
				margin-right: 10rpx;
			}
		}

		.search-box {
			width: 348rpx;
			padding-top: 12rpx;
		}

		.back {
			width: 40rpx;
			padding-top: 28rpx;
		}
	}

	.head-top {
		height: 258rpx;
		position: sticky;
		top: 0;
		background-color: #fff;
		z-index: 1000;
		overflow-y: overlay;
	}

	.transverse {
		.new-price-box {
			.integral-price-one {
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				color: #FF1C60;
				font-size: 20rpx;
				line-height: 48rpx;

				.inter-num {
					font-size: 28rpx;
					font-weight: bold;
					margin-right: 4rpx;
				}
			}

			.integral-price-two {
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				font-size: 20rpx;
				.red-word{
					color: #FF1C60;
					font-size: 22rpx;
					line-height: 42rpx;
					margin-left: 4rpx;
				}
                   .addIcon-two{
					   color: #FF1C60;
					   font-size: 22rpx;
					   line-height: 42rpx;
				   }
				   .jifen-tow{
					   font-size: 28rpx;
					   font-weight: bold;
					   color: #FF1C60;
					   line-height: 42rpx;
				   }
				.integral-price-left {
					display: flex;
					flex-direction: row;
					justify-content: flex-start;
					line-height: 42rpx;
					
					color: #FF1C60;

					.after-price {
						font-size: 28rpx;
						font-weight: bold;
					}
				}

				.integral-price-right {
					background: linear-gradient(to right, #FFA858, #E42548);
					color: #fff;
					line-height: 42rpx;
					padding-left: 10rpx;
					padding-right: 10rpx;

					border-radius: 12rpx;

				}

			}

			.old-price {
				font-size: 20rpx;
				text-decoration: line-through;
				color: #C2C2C2;
			}
		}


	}

	.price-box-two {
		.price{
			font-size: 24rpx;
			color: #FF4E47;
			font-weight: 600;
			text{
				font-size: 32rpx;
			}
		
		}
		.btn{
			width: 120rpx;
			border-radius: 20rpx;
			    text-align: center;
			    font-size: 24rpx;
			    padding-top: 4rpx;
			    padding-bottom: 4rpx;
				color: #fff;
				background: linear-gradient(to right, #FFA858, #E42548);
		}
	}
</style>